package com.simonall.gui.utils;

import java.awt.MenuItem;
import java.awt.PopupMenu;
import java.awt.SystemTray;
import java.awt.Toolkit;
import java.awt.TrayIcon;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.net.URL;

import javax.swing.SwingUtilities;

import com.simonall.enums.ResponseCodeEnum;
import com.simonall.gui.conf.Img;
import com.simonall.gui.event.ExitSystemListener;
import com.simonall.utils.BusinessException;

import cn.hutool.core.util.ObjectUtil;
import javafx.application.Platform;
import javafx.stage.Stage;
import lombok.extern.slf4j.Slf4j;

/**
 * 自定义系统托盘
 * 
 * @author simonall
 */
@Slf4j
public final class CustomTray {

	private static CustomTray instance;

	public static CustomTray getInstance() {
		if (instance == null) {
			instance = new CustomTray();
		}
		return instance;
	}

	static {
		// 程序不直接退出
		Platform.setImplicitExit(false);

		// 图标
		URL url = CustomTray.class.getResource(Img.ICON);
		trayIcon = new TrayIcon(Toolkit.getDefaultToolkit().getImage(url));
	}

	// 系统托盘图标
	private static TrayIcon trayIcon;
	private static MouseListener mouseListener;

	// 退出软件
	private static MenuItem exit;
	private static ActionListener exitListener;

	/**
	 * 构造器
	 * 
	 * @throws BusinessException
	 */
	public CustomTray() throws BusinessException {
		try {
			if (!SystemTray.isSupported()) { // 检查系统是否支持托盘
				System.err.println(Thread.currentThread().getStackTrace()[1].getClassName() + ":系统托盘不支持");
				return;
			}

			// 菜单
			exit = new MenuItem(ResponseCodeEnum.TXT_EXIT_MENU.getDesc());

			final PopupMenu popup = new PopupMenu();
			popup.add(exit);
			trayIcon.setPopupMenu(popup);
			trayIcon.setImageAutoSize(true);
			trayIcon.setToolTip(ResponseCodeEnum.APP_NAME.getDesc());
			SystemTray.getSystemTray().add(trayIcon);
		} catch (Exception e) {
			log.error(Thread.currentThread().getStackTrace()[1].getClassName() + ":系统添加失败", e);
		}
	}

	/**
	 * 监听
	 * 
	 * @param stage 窗口
	 */
	public void listen(Stage stage) throws BusinessException {
		// 添加监听
		if (exitListener != null) {
			exit.removeActionListener(exitListener);
		}
		exitListener = new ExitSystemListener();
		exit.addActionListener(exitListener);

		// 显示窗口
		mouseListener = new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				if (e.getButton() == MouseEvent.BUTTON1) {
					CustomTray.getInstance().show(stage);
				}
			}
		};
		trayIcon.addMouseListener(mouseListener);
	}

	/**
	 * 关闭窗口
	 * 
	 * @param stage
	 */
	public final void hide(Stage stage) {
		Platform.runLater(() -> {
			if (SystemTray.isSupported()) {
				stage.hide();
			} else {
				System.exit(0);
			}
		});
	}

	/**
	 * 显示界面
	 * 
	 * @param stage
	 */
	public final void show(Stage stage) {
		Platform.runLater(() -> {
			if (stage.isIconified()) {
				stage.setIconified(false);
			}
			if (!stage.isShowing()) {
				stage.show();
			}
			stage.toFront();
		});
	}

	/**
	 * 文本通知
	 * 
	 * @param text
	 */
	public static void inform(String text) {
		noneInform(ResponseCodeEnum.APP_NAME.getDesc(), text);
	}

	/**
	 * 简单通知
	 * 
	 * @param caption 标题
	 * @param text    内容
	 */
	public static void noneInform(String caption, String text) {
		instance.inform(caption, text, TrayIcon.MessageType.NONE);
	}

	/**
	 * 通知
	 * 
	 * @param caption 标题
	 * @param text    内容
	 * @param type    类型
	 */
	public void inform(String caption, String text, TrayIcon.MessageType type) {
		if (trayIcon == null || ObjectUtil.isEmpty(new String[] { caption, text })) {
			return;
		}
		SwingUtilities.invokeLater(() -> trayIcon.displayMessage(caption, text, type));
	}

}
